﻿using MediatR;
using SystemMHB.API.Application.Command.SupplierCommand;
using SystemMHB.Domian;
using SystemMHB.Domian.DTO;
using SystemMHB.Infrstructer.Interface;

namespace SystemMHB.API.Application.CommandHandlers.SupplierCommandHandlers
{
    public class SupplierShowCommandHandler:IRequestHandler<SupplierShowCommand, PagingDTO<Supplier>>
    {
        /// <summary>
        /// 供应商仓储接口
        /// </summary>
        ISupplierRepository supRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="supRep">供应商仓储接口</param>
        public SupplierShowCommandHandler(ISupplierRepository supRep)
        {
            this.supRep = supRep;
        }

        public async Task<PagingDTO<Supplier>> Handle(SupplierShowCommand request, CancellationToken cancellationToken)
        {
            var paging = new PagingDTO<Supplier>();
            var quest = from war in await supRep.GetAllAsync() where war.IsDel == false select war;
            paging.TotaCount = quest.Count();
            paging.pageCount = (int)Math.Ceiling((paging.TotaCount * 1.0) / request.pageSzen);
            paging.plist = quest.OrderBy(p => p.supplierID).Skip((request.pageiden - 1) * request.pageSzen).Take(request.pageSzen).ToList();
            return paging;
        }
    }
}
